﻿@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase

@{
    ViewBag.Title = "工具列表";
}

@section style{
    <!-- jqgrid-->
    <link href="~/Content/Css/plugins/jqgrid/ui.jqgrid.css" rel="stylesheet">

}

<div class="row-fluid clearfix bb-1 mb10" id="input_custom_searchbox">
    <div class="col-sm-5 pl0">
        <div class="input-group">
            <span class="input-group-addon">表名</span>
            <input type="text" class="form-control" id="txt_TableName">
        </div>
    </div>
</div>

<div class="app-toolbar">
    <button class="btn btn-white btn-sm" id="btn_GenCode"><span class="fa fa-plus"></span> 生成实体代码</button>
</div>

<!-- 列表 -->
<table id="grid_Table"></table>

@section scripts{
    <!-- jqGrid -->
    <script src="~/Content/Scripts/plugins/jqgrid/i18n/grid.locale-cn.js"></script>
    <script src="~/Content/Scripts/plugins/jqgrid/jquery.jqGrid.min.js"></script>
    <script>
        var gridTable = null;

        var tableList=@Html.Raw(ViewBag.TableList);

        $(function () {
            //初始化数据
            initData();

            //事件绑定
            bindEvents();
        });


        //初始化数据
        function initData() {
            $(tableList).each(function(i,item){
                item.Id = i;
            });

            //初始化表格控件
            gridTable = new Ywdsoft.Grid("grid_Table", {
                idField: "TableId",
                height: calcGridHeight(),
                multiselect: true,
                datatype: "local",
                data: tableList,
                rownumbers: true,
                colNames: ['表英文名', '表中文名'],
                colModel: [
                    {
                        name: 'TableName',
                        index: 'TableName'
                    },

                    {
                        name: 'TableCName',
                        index: 'TableCName',
                        width: 100
                    }]
            });
        }

        //计算表格高度
        function calcGridHeight() {
            return $(window).height() - 165;
        }

        //事件绑定
        function bindEvents() {
            //全局变量用于标识是否延时执行keyup事件
            var flag;
            $("#txt_TableName").on("keyup", function () {
                clearTimeout(flag);
                //延时400ms执行请求事件，如果感觉时间长了，就用合适的时间
                //只要有输入则不执行keyup事件
                flag = setTimeout(function () {
                    search();
                }, 400);
            });

            //搜索
            function search(){
                var key=$("#txt_TableName").val().trim().toLowerCase();
                var item = null,arrData = [];
                if(key.length==0){
                    arrData=tableList;
                }else{
                    for(var i=0,length=tableList.length;i<length;i++){
                        item=tableList[i];
                        if(item.TableName.toLowerCase().indexOf(key)>-1 || item.TableCName.toLowerCase().indexOf(key)>-1){
                            arrData.push(item);
                        }
                    }
                }
                //重新加载数据
                gridTable.reloadLocalData(arrData);
            };

            //生成代码
            $("#btn_GenCode").on("click",function(){
                //获取选中数据
                var $grid=$("#grid_Table");
                var rowids = $grid.jqGrid('getGridParam', 'selarrrow');
                if (rowids.length > 0) {
                    var arrTableName=[];
                    $(rowids).each(function(i,item){
                        var ret = $grid.jqGrid('getRowData', item);
                        arrTableName.push(ret.TableName);
                    });
                    //生成代码
                    $.download('/Tool/QuickCode',{TableList:arrTableName.join(",")},'post');
                } else {
                    layer.tips('请选择项', "#btn_GenCode", {
                        tips: 3
                    });
                }
            });
        }
    </script>
}